import {
  queryIndicators,
  queryAnalysis,
  queryStatistics,
} from '../services/index';

export default {
    namespace: 'maintenance',
  
    state: {
      AnalysisResource: {},
      IndicatorsResource: {},
      StatisticsResource: {},
    },
  
    effects: {
      *queryIndicators({ payload }, { call, put }) {
        const data = yield call(queryIndicators, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              IndicatorsResource: other,
            },
          })
        } else {
          throw data
        }
      },
      *queryAnalysis({ payload }, { call, put }) {
        // 解构 type，返回结果拼接
        const { type } = payload;
        const data = yield call(queryAnalysis, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              [`AnalysisResource${type}`]: other.features,
            },
          })
        } else {
          throw data
        }
      },
      *queryStatistics({ payload }, { call, put }) {
        const { type } = payload;
        const data = yield call(queryStatistics, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              [`StatisticsResource${type}`]: other.data,
            },
          })
        } else {
          throw data
        }
      },
    },
  
    reducers: {
      setState(state, action) {
        return {
            ...state,
            ...action.payload
        };
      }
    },
  }
  